<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="订单号" prop="orderNumber">
                <el-input v-model="queryParams.orderNumber" placeholder="请输入订单号" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="订单号" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号号" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="是否支付" prop="paid">
                <el-select v-model="queryParams.isPaid" clearable placeholder="请选择" size="small">
                    <el-option v-for="item in boolOptions" :key="item.dictValue" :label="item.dictLabel"
                        :value="item.dictValue.toString()"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="viporderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="订单号" align="center" prop="orderNumber" :show-overflow-tooltip="true" />
            <el-table-column label="用户" align="center" prop="userInfo" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <img :src="scope.row.userInfo.avatar" style="width:auto ;height:40px;margin-right:5px" />
                    {{ scope.row.userInfo.nickname }}
                </template>
            </el-table-column>
            <el-table-column label="订单总价" align="center" prop="totalPrice" :show-overflow-tooltip="true" />
            <el-table-column label="实际支付金额" align="center" prop="payPrice" :show-overflow-tooltip="true" />
            <el-table-column label="是否支付" align="center" prop="isPaid" :show-overflow-tooltip="true" />
            <el-table-column label="电话号码" align="center" prop="phone" :show-overflow-tooltip="true" />
            <el-table-column label="支付时间" align="center" prop="payAt" :show-overflow-tooltip="true" />
            <el-table-column label="赠送积分" align="center" prop="gainIntegral" :show-overflow-tooltip="true" />
            <el-table-column label="订单备注" align="center" prop="mark" :show-overflow-tooltip="true" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-permisaction="['vip:order:edit']" size="mini" type="text"
                        @click="handleUpdate(scope.row)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageIndex"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="订单号" prop="orderNumber">
                    <el-input v-model="form.orderNumber" placeholder="订单号" />
                </el-form-item>

                <el-form-item label="订单总价" prop="totalPrice">
                    <el-input v-model="form.totalPrice" placeholder="订单总价" />
                </el-form-item>
                <el-form-item label="实际支付金额" prop="payPrice">
                    <el-input v-model="form.payPrice" placeholder="实际支付金额" />
                </el-form-item>
                <el-form-item label="支付方式" prop="payType">
                    <el-input v-model="form.payType" placeholder="支付方式" />
                </el-form-item>

                <el-form-item label="是否支付" prop="isPaid">
                    <el-input v-model="form.isPaid" placeholder="是否支付" />
                </el-form-item>

                <el-form-item label="赠送积分" prop="gainIntegral">
                    <el-input v-model="form.gainIntegral" placeholder="赠送积分" />
                </el-form-item>
                <el-form-item label="用户电话" prop="phone">
                    <el-input v-model="form.phone" placeholder="用户电话" />
                </el-form-item>
                <el-form-item label="管理员备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="管理员备注" />
                </el-form-item>

                <el-form-item label="备注" prop="mark">
                    <el-input v-model="form.mark" placeholder="备注" />
                </el-form-item>

                <el-form-item label="创建时间" prop="createdAt">
                    <el-input v-model="form.createdAt" placeholder="创建时间" />
                </el-form-item>
                <el-form-item label="支付时间" prop="payAt">
                    <el-input v-model="form.payAt" placeholder="支付时间" />
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    listVipOrder,
    getVipOrder,
    delVipOrder,
    addVipOrder,
    updateVipOrder
} from "@/api/vip/order";

export default {
    name: "Config",
    props: {
        userId: {
            type: Number,
            default: () => {
                return 0;
            }
        },
    },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 类型数据字典
            typeOptions: [],
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageIndex: 1,
                pageSize: 10,
                orderId: undefined,
                orderNumber: undefined,
                userId: undefined,
                paid: undefined
            },
            // 表单参数
            form: {
                userId: this.userId
            },
            // 表单校验
            rules: {
                orderId: [
                    {
                        required: true,
                        message: "订单ID不能为空",
                        trigger: "blur"
                    }
                ],
                orderNumber: [
                    {
                        required: true,
                        message: "订单号不能为空",
                        trigger: "blur"
                    }
                ],
                paid: [
                    {
                        required: true,
                        message: "是否支付不能为空",
                        trigger: "blur"
                    }
                ]
            },
            //列表数据
            viporderList: [],
            //是否
            boolOptions: []
        };
    },
    created () {
        this.getList();

        this.boolOptions = this.$store.getters.parameter.sys_bool;
    },
    watch: {
        //监听子控件内容的改动,发送给父级组件
        userId: {
            handler (newValue, oldValue) {
                this.queryParams.userId = this.userId;
                this.queryParams.phone = this.phone
                this.getList();
            },
            deep: true
        },
        phone: {
            handler (newValue, oldValue) {
                this.queryParams.userId = this.userId;
                this.queryParams.phone = this.phone
                this.getList();
            },
            deep: true,
        },
    },
    methods: {
        /** 查询参数列表 */
        getList () {
            this.loading = true;
            this.queryParams.userId = this.userId;
            listVipOrder(
                this.addDateRange(this.queryParams, this.dateRange)
            ).then(response => {
                this.viporderList = response.data.list;
                this.total = response.data.count;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                couponId: undefined,
                couponPrice: undefined,
                createdAt: undefined,
                deductionPrice: undefined,
                deletedAt: undefined,
                gainIntegral: undefined,
                isChannel: undefined,
                isRemind: undefined,
                mark: undefined,
                orderId: undefined,
                orderNumber: undefined,
                isPaid: undefined,
                payAt: undefined,
                payPrice: undefined,
                payType: undefined,
                remark: undefined,
                status: undefined,
                totalPrice: undefined,
                updatedAt: undefined,
                useIntegral: undefined,
                userId: this.userId,
                phone: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageIndex = 1;
            // this.queryParams.userId = this.userId;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset();
            this.open = true;
            this.title = "添加VIP充值订单表";
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.orderId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset();
            const orderId = row.orderId || this.ids;
            getVipOrder(orderId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "查看VIP充值订单";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.orderId !== undefined) {
                        updateVipOrder(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addVipOrder(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const Ids = row.orderId || this.ids;
            this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(function () {
                    return delVipOrder(Ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function () { });
        }
    }
};
</script>
